//
//  XMChatTextCell.m
//  XMJJ_zmy
//
//  Created by 丁浩 on 2025/7/5.
//

#import "XMChatTextCell.h"

@interface XMChatTextCell ()

@property (nonatomic, strong) UIView *contentBgView;
@property (nonatomic, strong) UILabel *contentLabel;

@end

@implementation XMChatTextCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    self.backgroundColor = kColorHEX(0xF7F7F7);
    [self addSubview:self.contentBgView];
    [_contentBgView addSubview:self.contentLabel];
}

- (void)setContentWithMessage:(V2TIMMessage *)message
                 senderAvatar:(NSString *)senderAvatarImg
               receiverAvatar:(NSString *)receiverAvatarImg {
    self.isSelf = message.isSelf;
    if (message.isSelf) {
        [self.avatarImg sd_setImageWithURL:[NSURL URLWithString:senderAvatarImg] placeholderImage:ImageNamed(@"mine_avatar_icon")];
        [self setSenderMsgContentWithMessage:message];
    }
    else {
        [self.avatarImg sd_setImageWithURL:[NSURL URLWithString:receiverAvatarImg] placeholderImage:ImageNamed(@"mine_avatar_icon")];
        self.contentBgView.x = self.avatarImg.maxX + KScaleWidth(10.0);
        self.contentLabel.x = KScaleWidth(10.0);
        [self setReceiverMsgContentWithMessage:message];
    }
}

- (void)setSenderMsgContentWithMessage:(V2TIMMessage *)message {
    switch (message.elemType) {
        case V2TIM_ELEM_TYPE_TEXT: {
            CGFloat width = [message.textElem.text getWidthWithFont:KScaleWidth(14.0)] + KScaleWidth(18.0)*2;
            if (width > MAXWIDTH) {
                CGFloat height = [message.textElem.text getHeightWithFont:KScaleWidth(14.0) withWidth:MAXWIDTH - KScaleWidth(18.0)*2];
                _contentBgView.x = self.avatarImg.width + KScaleWidth(10.0)*2;
                _contentBgView.width = MAXWIDTH;
                _contentBgView.height = height + KScaleWidth(20.0);
//                [_contentLabel setCornerRadius:KScaleWidth(18.0)];
                _contentLabel.height = height + KScaleWidth(20.0);
                _contentLabel.width = MAXWIDTH - KScaleWidth(18.0)*2;
                _contentLabel.x = _contentBgView.width - KScaleWidth(10.0) - _contentLabel.width;
//                _contentLabel.textAlignment = NSTextAlignmentCenter;
                _contentLabel.text = message.textElem.text;
            }
            else {
                _contentBgView.width = MAX(width, KScaleWidth(44.0));
                _contentBgView.height = KScaleWidth(44.0);
                _contentBgView.x = kScreenWidth - KScaleWidth(10.0)*2 - self.avatarImg.width - _contentBgView.width;
                
                _contentLabel.width = _contentBgView.width - KScaleWidth(18.0)*2 + KScaleWidth(5.0);
                _contentLabel.text = message.textElem.text;
//                _contentLabel.textAlignment = NSTextAlignmentCenter;
            }
            [_contentBgView setCornerRadius:KScaleWidth(10.0)];
        }
            break;
            
        default:
            break;
    }
}

- (void)setReceiverMsgContentWithMessage:(V2TIMMessage *)message {
    CGFloat width = [message.textElem.text getWidthWithFont:KScaleWidth(14.0)] + KScaleWidth(18.0)*2;
    if (width > MAXWIDTH) {
        CGFloat height = [message.textElem.text getHeightWithFont:KScaleWidth(14.0) withWidth:MAXWIDTH - KScaleWidth(18.0)*2];
        _contentBgView.width = MAXWIDTH;
        _contentBgView.height = height + KScaleWidth(20.0);
        _contentLabel.height = height + KScaleWidth(20.0);
        _contentLabel.width = MAXWIDTH - KScaleWidth(18.0)*2;
        _contentLabel.text = message.textElem.text;
    }
    else {
        _contentBgView.width = MAX(width, KScaleWidth(44.0));
        _contentBgView.height = KScaleWidth(44.0);
        
        _contentLabel.width = _contentBgView.width - KScaleWidth(18.0)*2 + KScaleWidth(5.0);
        _contentLabel.text = message.textElem.text;
    }
    [_contentBgView setCornerRadius:KScaleWidth(10.0)];
}

- (UIView *)contentBgView {
    if (!_contentBgView) {
        _contentBgView = [[UIView alloc] initWithFrame:CGRectMake(self.avatarImg.maxX + KScaleWidth(10.0), self.avatarImg.y, kScreenWidth - self.avatarImg.maxX - KScaleWidth(10.0) - self.avatarImg.width - KScaleWidth(10.0), self.avatarImg.height)];
        _contentBgView.backgroundColor = kColorHEX(0xffffff);
        [_contentLabel setCornerRadius:_contentBgView.height/2];
    }
    return _contentBgView;
}

- (UILabel *)contentLabel {
    if (!_contentLabel) {
        _contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(18.0), 0, self.contentView.width - KScaleWidth(18.0)*2, _contentBgView.height) font:KFontSize(14.0) textColor:kColorHEX(0x333333)];
        _contentLabel.numberOfLines = 0;
//        _contentLabel.backgroundColor = kColorHEX(0x00ffff);
    }
    return _contentLabel;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
